package com.youshang520i.nacosserviceconsume.controller;

//import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.youshang520i.nacosserviceconsume.service.ConsumeService;
import com.youshang520i.nacosserviceconsume.service.TransactionConsumeService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author youshang
 */
@RestController
public class ConsumeController {

    @Autowired
    private ConsumeService consumeService;
    @Autowired
    private TransactionConsumeService transactionConsumeService;

    /**
     *  fallback 只有在当前方法，满足nacos配置文件中设置的限流条件的时候才会触发
     * @param str
     * @return
     */
    @GetMapping("/index")
    @SentinelResource(value = "consume-index",fallback = "error")
    public String index(@RequestParam("str") String str) {
//        String index =  "";
        String index = consumeService.index(str);
        System.out.println("请求参数 ： " + str);
        return index;
    }


    public String error(String str) {
        return str + " : 操作过于频繁，请稍后再试！！！";
    }


    /**
     * GlobalTransactional 使用 seata分布式事务
     * @param num
     * @return
     */
    @GlobalTransactional
    @GetMapping(value = "/tIndex/{num}")
    public Boolean tIndex(@PathVariable("num") Integer num){
        return transactionConsumeService.tIndex(num);
    }



}
